

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户列表</title>

    <%--图片 和下拉框--%>
    <script type="text/html" id="productTpl">
        <img width="50"  src="http://localhost:8082/img/{{d.img_path}}">  {{d.sname}}
    </script>

</head>
<body>

<%--   新增修改的页面--%>
<div id="addWinpp"  style="display: none;padding-top: 5px;" >
    <form id="ptypeBiaodan" class="layui-form" action="" lay-filter="testForm">
        <br>  <br>
        <div class="layui-form-item">
            <label class="layui-form-label">商品类别</label>
            <div class="layui-input-block">
                <input type="hidden" name="tid" />
                <%--placeholder="学生姓名"--%>
                <input type="text" name="tname" required  lay-verify="required"  lay-verify="required" lay-reqtext="商品类别不能为空" placeholder="请输入商品类别" value="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <br>  <br>  <br>  <br>  <br>  <br>  <br>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keyword" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
                <div class="layui-col-md1">
                    <button id="toEqq" class="layui-btn">增加</button>
                </div>
            </div>
        </fieldset>

        <table  id="dataTable" lay-filter="test"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <%--<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="addRole">更改商品类别</a>--%>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>


    </div>
</div>

<script>
    layui.use(['layer', 'form','table','laydate','upload'], function(){
        var layer = layui.layer
            ,form = layui.form,
            table=layui.table
            , $=layui.jquery
            ,laydate = layui.laydate
            ,upload=layui.upload
        ;


        //
        $(function () {
            $("#addWinpp").appendTo("body");
        });


        table.render({
            elem: '#dataTable',
            url: 'ptype/pListAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'tid', width: 160, title: '商品类别编号', sort: true},
                {field: 'tname', width: 130, title: '商品类别名称'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('dataTable', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });


        // alert($); 测试  Layui的jquery是否成功
        // 使用jquery 给按钮动态绑定事件  添加按钮
        $("#toEqq").bind('click',function (){
            //重置表单数据
            $("#ptypeBiaodan").get(0).reset();
            //弹出界面
            layer.open({
                type: 1,
                area: ['500px', '400px'],
                title:'新增商品',
                content: $('#addWinpp')
            });
        });


        //   保存按钮   from  表单 提交按钮的监听器监听提交
        form.on('submit(formDemo)', function(data){
            //相后台发送 ajax 请求
            // console.info(data);
            $.post("ptype/saveOrUpdate",data.field,function (data) {
                // alert(data)
                //如果操作成功
                if(data.success){
                    //关闭窗口
                    layer.closeAll();
                    //刷新数据   查询出来的数据刷新
                    table.reload('dataTable');
                }else{
                    layer.msg(data.error);
                }
            });
            return false;
        });


        //监控表格的工具条中的按钮的单击事件    table.on('tool(test)', function(obj)
        table.on('tool(test)', function(obj){
            if(obj.event=="edit"){
                // alert("点击了编辑");
                //把弹窗打开
                layer.open({
                    type: 1,
                    area: ['1000px', '600px'],
                    title:'修改商品',
                    content: $('#addWinpp')
                });
                //回显数据   主表上面加一个  lay-filter="testForm"
                form.val("testForm",obj.data);

            }else if(obj.event=="delete"){
                // alert("点击了删除");
                layer.confirm('是否确认删除商品?', {icon: 3, title:'提示'}, function(index){
                    //往后台发送ajax请求 删除数据
                    $.post("ptype/delete",{tid:obj.data.tid},function (data) {
                        // alert(7878)
                        if(data.success){
                            // 关闭当前弹窗
                            // alert(1)
                            layer.close(index);
                            // alert(2)
                            //刷新数据
                            table.reload('dataTable');
                        }else{
                            layer.msg(data.error);
                        }

                    })

                });
            }
        });
    });
</script>
</body>
</html>
